package cn.oscar.service.imp;

import cn.hutool.core.bean.BeanUtil;
import cn.oscar.mapper.AdminMapper;
import cn.oscar.service.AdminService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import commonUtils.dto.AdminDTO;
import commonUtils.entities.Administrator;
import commonUtils.entities.Restful;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
public class AdminServiceImp extends ServiceImpl<AdminMapper,Administrator> implements AdminService {

    @Autowired
    private AdminMapper adminMapper;

    @Override
    public Restful<Administrator> login(HttpServletRequest request, Administrator administrator) {
        String username = administrator.getUsername();

        //1.将页面提交的密码进行MD5加密处理
        String password = administrator.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        //2.根据用户名查询数据库
        LambdaQueryWrapper<Administrator> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Administrator::getUsername,username);
        Administrator admin = getOne(queryWrapper);
        //3.查不到结果返回登录失败结果
        if (admin == null){
            return Restful.error("登录失败");
        }
        //4.密码比对，如果不一致返回失败结果
        if(!(admin.getPassword().equals(password))){
            return Restful.error("登录失败");
        }
        //5.状态比对，是否是可登录状态
        if (admin.getStatus() == 0){
            return Restful.error("账号已禁用");
        }
        //6.登录成功，将员工ID存入session并返回登录成功结果
        //原因：用户登录后直到退出操作时，要存入这次的登录员工的ID信息
        request.getSession().setAttribute("admin",admin.getId());
        return Restful.success(admin);

    }

    @Override
    public Restful<String> logout(HttpServletRequest request) {
        request.getSession().removeAttribute("admin");
        return Restful.success("退出成功");
    }

    @Override
    public Restful<String> addAdmin(Administrator administrator) {
        log.info("新增管理员，管理员信息：" + administrator.toString());
        //补全新建管理员的默认信息,统一初始密码，进行加密传输
        administrator.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        save(administrator);
        return Restful.success("添加成功");
    }

    @Override
    public Restful<Page<AdminDTO>> selectByPage(int page, int pageSize, String name) {
        log.info("page={},pageSize={},name={}",page,pageSize,name);
        //构造分页构造器
        Page<Administrator> pageInfo = new Page<>(page,pageSize);
        Page<AdminDTO> pageInfoDto = new Page<>();

        //构造分页查询
        LambdaQueryWrapper<Administrator> queryWrapper = new LambdaQueryWrapper<>();
        //添加过滤条件

        queryWrapper.like(name != null && !name.trim().equals(""),Administrator::getName,name);

        //添加排序条件 order by
        queryWrapper.orderByDesc(Administrator::getUpdateTime);

        //执行查询
        page(pageInfo,queryWrapper);
        //分页查询数据复制给dto
        BeanUtil.copyProperties(pageInfo,pageInfoDto,"records");

        List<Administrator> records = pageInfo.getRecords();
        List<AdminDTO> recordsDto = records.stream().map((admin)->{
            AdminDTO adminDto = new AdminDTO();
            BeanUtil.copyProperties(admin,adminDto);
//            log.info("admin的信息是：" + admin);
//            log.info("adminDto的信息是：" + adminDto);
            return adminDto;
        }).collect(Collectors.toList());

        pageInfoDto.setRecords(recordsDto);

        return Restful.success(pageInfoDto);
    }

    @Override
    public Restful<String> updateAdmin(Administrator administrator) {
        updateById(administrator);
        return Restful.success("管理员修改信息成功");
    }

    @Override
    public Restful<Administrator> selectById(int id) {
        //        System.out.println("根据id查询管理员信息为：" + id);
        log.info("根据id查询员工信息");
        Administrator admin = getById(id);
        System.out.println("查询的用户是："+admin);
        if (admin != null){
            return Restful.success(admin);
        }
        return Restful.error("查不到该用户信息");

    }

    @Override
    public List<Administrator> selectAdminList() {
        return adminMapper.selectAdminList();
    }


}
